<?php
// +----------------------------------------------------------------------
// | Author: 发仔 <i@fazi.me> 
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\Db;

class Driver Extends Base
{
    protected $id = 'driver_id';
	protected $table = 'driver';
	protected $joins = [['car c','c.car_id = m.car_id','left'],['car_rental cr','cr.car_rental_id = m.car_rental_id','left']];
	protected $field = 'm.*,c.number,cr.name AS car_rental_name';
	
	
	public function stat() {
		
		//搜索
		$start = input('param.start','1970-01-01');
		$end = input('param.end',date('Y-m-d'));
		$start = $start ?: '1970-01-01';
		$end = $end ?: date('Y-m-d',strtotime("+1 days"));
		
		//时间段
		$time = input('param.time');
		if($time == 'day') {
			$start = date('Y-m-d');
		} elseif($time == 'month') {
			$start = date('Y-m-01');
		} elseif($time == 'year') {
			$start = date('Y-01-01');
		}
		
		$company = input('param.car_rental_id');
		$name = input('param.name');
		$count = [];
		//用车公司搜索
		
		$order = Db::name('order')->alias('o')->whereNull('o.delete_time')->where('o.order_time','>=',$start)->where('o.order_time','<=',$end);
		$driver = Db::name('driver')->alias('d')->whereNull('d.delete_time');
		//
//		$json = file_get_contents('http://new.jjcxfw.com/api/driver.index/getRedisDriver');
//		$json = json_decode($json,1);
		$count['online'] = count($json['data']);
		
		if($name) {
			$driver->where('name','like',"%{$name}%");
			$order->join('driver d','d.driver_id = o.driver_id')->where('d.name','like',"%{$name}%");
 		} elseif($company) {
			$driver->join('car_rental cr','cr.car_rental_id = d.car_rental_id')->where('cr.car_rental_id',$company);
			$order->join('driver d','d.driver_id = o.driver_id')->join('car_rental cr','cr.car_rental_id = d.car_rental_id')->where('cr.car_rental_id',$company);
		}
		
		//司机
		$drivers = $driver->select();
		$orders = $order->select();

		//总数
		$count['qty'] = count($drivers);
		$driver_ids = array_column($drivers,'driver_id');
		foreach($drivers As $d) {
			if( $d['work_state'] > 1 ) {
				$count['online']++;
			}
		}
		//在线
//		halt($count);
		//运行里程
		$count['km'] =   array_sum( array_column($orders,'actual_mileage'));
		$count['km_last'] = $count['km'] - intval($count['km']*0.05);
		$count['water'] = array_sum( array_column($orders,'settlement_price'));
		$count['water_last'] = $count['water'] - intval($count['water']*0.05);
		//接单数
		$count['orders'] = count($orders);
		
		foreach($orders AS $o) {
			if($o['order_state'] == 12) {
				$count['cancel']++;
			} elseif($o['order_state'] == 11) {
				$count['finish']++;
			}
		}
		
		$count['orders_last'] = $count['orders'] - intval($count['orders']*0.05);
		$count['cancel_last'] = $count['cancel'] - intval($count['cancel']*0.05);
		$count['finish_last'] = $count['finish'] - intval($count['finish']*0.05);
		
		//投诉
		$count['complaint'] = Db::name('complaint')->count();
		$count['complaint_last'] = $count['complaint'] - intval($count['complaint']*0.05);
//		halt($count);
		$this->assign('item',$count);
		//司机
		return $this->out();
	}
	
	//绑定
	public function bind() {
		
		$driver_id = input('post.driver_id');
		$car_id = input('post.car_id');
		$rslt = Db::name('Driver')->where('driver_id',$driver_id)->setField('car_id',$car_id);
		
		return $this->res($rslt);
	}
	//解除绑定
	public function unbind() {
		
		$driver_id = input('post.driver_id');
		$car_id = input('post.car_id');
		$rslt = Db::name('Driver')->where('driver_id',$driver_id)->setField('car_id',0);
		
		return $this->res($rslt);
	}
	
	protected function info() {
		$driver_id = input('post.driver_id');
		$car_id = input('post.car_id');
		
		$this->relation = ['driver_id'=>$driver_id,'car_id'=>$car_id];
		
		//是否已经绑定
		$this->bind = Db::name('DriverCar')->where($this->relation)->find();
		
	}
	
	protected function options($item = []) {
		
		$this->assign('work_states',config('base.work_states'));
		$this->assign('languages',TplOptions( config('base.languages'),$item['language'] ));
		
		$languages = config('base.languages');
		if(!empty($item['language'])){
			$temp = explode(',',$item['language']);
			foreach($temp AS $l) {
				$item['languages'][] = $languages[$l];
			}
			$item['languages'] = implode(',',$item['languages']);
		}
		
		return $item;
	}
}
